//SUMMARY:  This do file replicates findings in "Trade as Villain" from the Yougov panel data

clear all
set more off

*Set classpath here:
cd "XXX"

use "YG panel with region data.dta", clear

tsset case_id year
sort case_id year

svyset case_id [pweight=weight]

//We define sets of covariates here:
global indivCov "age age2 female i.educ4cat isWhite i.incomeScaleB"

* Within individual analysis
gen trade2011 = tradePolicy if year == 2011
bysort case_id: egen tp11 = max(trade2011)
gen trade2012 = tradePolicy if year == 2012
bysort case_id: egen tp12 = max(trade2012)
gen trade2016 = tradePolicy if year == 2016
bysort case_id: egen tp16 = max(trade2016)
gen chgTrade_1116 = tp16 - tp11
gen chgTrade_1216 = tp16 - tp12
gen chgTrade = chgTrade_1116
replace chgTrade = chgTrade_1216 if chgTrade == . & chgTrade_1216 != .


//FIGURE 3(a):  Within-respondent change in trade preferences, by meritocracy beliefs:
reg chgTrade merit  $indivCov if year == 2016
margins, at(merit = (0)) post
est sto nonMerit
reg chgTrade merit  $indivCov if year == 2016
margins, at(merit = (1)) post
est sto yesMerit
coefplot (nonMerit, label(Non-meritocrats)) (yesMerit, label(Meritocrats)), yline(0) bycoefs recast(bar) vertical barwidth(.3) bargap(-10) fcolor(*.5) ciopts(recast(rcap)) citop format(%9.2f) ytitle(Within-indiv. change in trade pref.) xlabel("")
graph export "Output/Within_indiv_chgTrade(merit).png", as(png) replace

//FIGURE 3(b):  Within-respondent change in trade preferences, by meritocracy beliefs and social mobility:
reg chgTrade merit##childBetter $indivCov if year == 2016
margins merit, at(childBetter = (1)) post
est sto chgtradeBetter
reg chgTrade merit##childBetter $indivCov if year == 2016
margins merit, at(childBetter = (0)) post
est sto chgtradeWorse

coefplot (chgtradeBetter, label(High IME)) (chgtradeWorse, label(Low IME)), yline(0) bycoefs recast(bar) vertical barwidth(.3) bargap(-10) fcolor(*.5) ciopts(recast(rcap)) citop format(%9.2f) ytitle(Within-indiv. change in trade pref.) xlabel("")
graph export "Output/Within_indiv_chgTrade(meritXmobil).png", as(png) replace

//FIGURE 4:  Meritocracy beliefs and local economic mobility:
*NOTE:  Because we lack zipcode data in 2012 (to match regional data), we instead take difference in trade preferences from 2011-2016 as DV here
reg chgTrade_1116 merit##c.prob_p1_k5 $indivCov if year == 2016, cluster(stateCode)
margins, dydx(merit) at(prob_p1_k5 = (0.02(.01).15))
marginsplot, yline(0) ytitle("Change in trade pref., 2011-2016") title("Meritocracy, local income mobility, and changing trade views") addplot(hist prob_p1_k5 if prob_p1_k5 <= .15, lcolor(gs13) fcolor(none) xtitle(Regional intergenerational income mobility (county)) yaxis(2) xlabel(.02(.01).15) yscale(alt range(0 1) axis(2))  leg(off))
graph export "Output/Within_indiv_chgTrade(meritXchetty).png", as(png) replace

//APPENDIX OUTPUT:
//FIGURE A1:  Average US trade preferences over time:
*This omits gap between 2012 and 2016
gen yearDisp = year-2007
//Move 2016 closer
replace yearDisp = 7 if yearDisp == 9
lab def yrDispLab 0 "2007" 1 "2008" 2 "2009" 3 "2010" 4 "2011" 5 "2012" 6 "~" 7 "2016"
lab val yearDisp yrDispLab

reg tradePolicy i.yearDisp, cluster(case_id)
margins yearDisp
marginsplot, xdim(yearDisp) xtitle(Year) ytitle(Trade pref) title(Average US trade preferences over time) recast(bar) xlabel(0 "2007" 1 "2008" 2 "2009" 3 "2010" 4 "2011" 5 "2012" 6 "~" 7 "2016")
graph export "Output/Trade_pref_over_time.png", as(png) replace

//FIGURE A2:  Trade preferences in 2012 vs 2016, by meritocracy:
reg tradePolicy i.merit##year $indivCov, cluster(case_id)
margins merit, at(year = (2012)) post
est sto merit2012
reg tradePolicy i.merit##year $indivCov, cluster(case_id)
margins merit, at(year = (2016)) post
est sto merit2016
coefplot (merit2012, label(2012) color(green) ciopts(lcolor(green) recast(rcap))) (merit2016, label(2016) color(orange) ciopts(lcolor(orange) recast(rcap))), bycoefs byopts(rows(1) title("Views on trade, by meritocracy beliefs")) recast(bar) vertical barwidth(.3) bargap(-10) fcolor(*.5) ciopts(recast(rcap)) citop format(%9.2f) ytitle(Trade pref.) xlabel("") 
graph export "Output/Trade_pref_2012_vs_2016(meritocracy).png", as(png) replace

//FIGURE A3:  Trade preferences in 2012 vs 2016, by meritocracy and social mobility:
reg tradePolicy merit##childBetter##year $indivCov, cluster(case_id)
margins merit#childBetter, at(year = (2012)) post
est sto triple2012
reg tradePolicy merit##childBetter##year $indivCov, cluster(case_id)
margins merit#childBetter, at(year = (2016)) post
est sto triple2016
coefplot (triple2012, label(2012) color(green) ciopts(lcolor(green) recast(rcap))) (triple2016, label(2016) color(orange) ciopts(lcolor(orange) recast(rcap))), bycoefs recast(bar) coeflabels(, interaction(" x ")) byopts(title("Views on trade (social mobility X meritocracy beliefs)")) vertical barwidth(.3) bargap(-10) fcolor(*.5) ciopts(recast(rcap)) citop format(%9.2f) ytitle(Trade pref.) xlabel("") 
graph export "Output/Trade_pref_2012_vs_2016(mobilXmerit).png", as(png) replace

//TABLE A1:  Trade views over time
reg tradePolicy i.merit##year $indivCov, cluster(case_id)
outreg2 using "Output/panelResults", replace tex(fra) ctitle(Expand trade) nocons bdec(3) label
reg tradePolicy merit##childBetter##year $indivCov, cluster(case_id)
outreg2 using "Output/panelResults", append tex(fra) ctitle(Expand trade) nocons bdec(3) label

//TABLE A2: Within-individual change in trade preferences:
reg chgTrade merit##childBetter $indivCov if year == 2016
outreg2 using "Output/withinIndiv", replace tex(fra) ctitle(W/in indiv.) nocons bdec(3) label

//FIGURE A4:  Within-individual change in trade preference, by change in IME:
bysort case_id: gen chgIME = childBetter[_n] - childBetter[_n-1]
gen betterIME = (chgIME == 1) if chgIME != .
reg chgTrade merit##betterIME $indivCov if year == 2016
margins merit, at(betterIME = (1)) post
est sto chgTradeIMEbetter
reg chgTrade merit##betterIME $indivCov if year == 2016
margins merit, at(betterIME = (0)) post
est sto chgTradeIMEworse

coefplot (chgTradeIMEbetter, label(Better IME)) (chgTradeIMEworse, label(Worse IME)), yline(0) bycoefs recast(bar) vertical barwidth(.3) bargap(-10) fcolor(*.5) ciopts(recast(rcap)) citop format(%9.2f) ytitle(Within-indiv. change in trade pref.) xlabel("")
graph export "Output/Within_indiv_chgTrade(meritXchgIME).png", as(png) replace

//FIGURE A5:  Trade preferences in 2012 vs 2016, by meritocracy beliefs (individual FE)
*NOTE:  Meritocracy question was only asked in 2012 and 2016; in order to leverage full panel for FE regression, we "impute" meritocracy views to earlier periods
bysort case_id: egen meritImp = mode(poorAreLazy)
replace meritImp = poorAreLazy if poorAreLazy != .
lab val meritImp meritLab
xtreg tradePolicy meritImp##year $indivCov, fe cluster(case_id)
margins meritImp, at(year = (2012)) post
est sto panel2012
xtreg tradePolicy meritImp##year $indivCov, fe cluster(case_id)
margins meritImp, at(year = (2016)) post
est sto panel2016
coefplot (panel2012, label(2012)) (panel2016, label(2016)), yline(0) bycoefs recast(bar) vertical barwidth(.3) bargap(-10) fcolor(*.5) ciopts(recast(rcap)) citop format(%9.2f) ytitle(Expand trade) xlabel("")
graph export "Output/Trade_pref_2012_vs_2016(merit)_indivFE.png", as(png) replace


//FIGURE A6:  Trade preferences in 2012 vs 2016, by meritocracy beliefs and social mobility (individual FE):
xtreg tradePolicy meritImp##childBetter##year $indivCov, fe cluster(case_id)
margins meritImp#childBetter, at(year = (2012)) post
est sto triple2012indivFE
xtreg tradePolicy meritImp##childBetter##year $indivCov, fe cluster(case_id)
margins meritImp#childBetter, at(year = (2016)) post
est sto triple2016indivFE
coefplot (triple2012indivFE, label(2012) color(blue) ciopts(lcolor(blue) recast(rcap))) (triple2016indivFE, label(2016) color(red) ciopts(lcolor(red) recast(rcap))), bycoefs recast(bar) coeflabels(, interaction(" x ")) byopts(title("Views on trade (social mobility X meritocracy beliefs)")) vertical barwidth(.3) bargap(-10) fcolor(*.5) ciopts(recast(rcap)) citop format(%9.2f) ytitle(Trade pref.) xlabel("") 
graph export "Output/Trade_pref_2012_vs_2016(meritXchild)_indivFE.png", as(png) replace

//TABLE A3:  Individual FE regression
xtreg tradePolicy meritImp##year $indivCov, fe cluster(case_id)
outreg2 using "Output/indivFE", replace tex(fra) ctitle(Trade pref.) nocons bdec(3) label
xtreg tradePolicy meritImp##childBetter##year $indivCov, fe cluster(case_id)
outreg2 using "Output/indivFE", append tex(fra) ctitle(Trade pref.) nocons bdec(3) label

//FIGURE A7:  Regional and perceived income mobility
* NOTE: Here, we rely on "imputed" zipcode in 2012 for respondents for which we had same zipcode in survey prior to 2012 as in 2016
reg childFinFuture c.e_rank_b##year $indivCov county_unemp county_avgPay, cluster(stateCode)
margins, dydx(e_rank_b) at(year = (2007 2008 2009 2010 2011 2012 2016))
marginsplot, yline(0) ytitle(Child financial future) title("Effect of local income mobility on IME, by year")
graph export "Output/subjObjMobility.png", as(png) replace

//TABLE A4:  Within individual change in trade preferences and regional income mobility:
reg chgTrade_1116 merit##c.prob_p1_k5 $indivCov if year == 2016, cluster(stateCode)
outreg2 using "Output/geogResults", replace tex(fra) ctitle(Chg. trade) nocons bdec(3) label
reg chgTrade_1116 merit##c.e_rank_b $indivCov if year == 2016, cluster(stateCode)
outreg2 using "Output/geogResults", append tex(fra) ctitle(Chg. trade) nocons bdec(3) label

//TABLE A8:  Trump vote and change in trade preferences
reg trumpVoter chgTrade
outreg2 using "Output/trumpVote", replace tex(fra) ctitle(Vote Trump) nocons bdec(3) label
reg trumpVoter chgTrade $indivCov i.partyID_3pt
outreg2 using "Output/trumpVote", append tex(fra) ctitle(Vote Trump) nocons bdec(3) label

//TABLE A10:  Correlates of meritocracy views:
reg merit $indivCov i.partyID_3pt, cluster(case_id)
outreg2 using "Output/meritCorrelates", replace tex(fra) ctitle(Poor lazy) nocons bdec(3) label
